//
//  MTTools.swift
//  Clock
//
//  Created by 方强 on 16/7/1.
//  Copyright © 2016年 MT. All rights reserved.
//

import UIKit
import CoreBluetooth

public class MTTools: NSObject {
    // byte转换成字符串
    public static func convertByteToString(byteToConvert:UInt8) -> String{
        return String(format: "%02X", byteToConvert);
    }
    
    // byte[]转换成字符串
    public static func convertBytearrayToString(data:[UInt8]?) -> String?{
        if(data == nil){
            return nil;
        }
        
        var str = "";
        
        for i in data! {
            str += MTTools.convertByteToString(byteToConvert: i);
        }
        
        return str;
    }
    
    // Data转换成字符串
    public static func convertDataToString(data:Data?) -> String?{
        if(data == nil){
            return nil;
        }
        
        var str = "";
        
        for i in data! {
            str += " \(MTTools.convertByteToString(byteToConvert: i))";
        }
        
        return str;
    }
    
    // string转换成byte
    public static func convertCharToByte(hvalue_const:UInt8, lvalue_const:UInt8) -> UInt8 {
        var hvalue = hvalue_const;
        var lvalue = lvalue_const;
        
        if (!(((hvalue >= 0x30) && (hvalue <= 0x39)) || ((hvalue >= 0x61) && (hvalue <= 0x66))
				|| ((hvalue >= 0x41) && (hvalue <= 0x46)))) {
            return 0;
        }
        if (!(((lvalue >= 0x30) && (lvalue <= 0x39)) || ((lvalue >= 0x61) && (lvalue <= 0x66))
				|| ((lvalue >= 0x41) && (lvalue <= 0x46)))) {
            return 0;
        }
        if((lvalue >= 0x61) && (lvalue <= 0x66)){  // 小写转换成大写
            lvalue = lvalue-0x20;
        }
        if((hvalue >= 0x61) && (hvalue <= 0x66)){
            hvalue = hvalue-0x20;
        }
    
        var value:UInt8 = 0;
    
        if ((hvalue <= 0x39) && (hvalue >= 0x30)) {
            value = UInt8(((hvalue - 0x30) * 16) & 0xFF);
        } else {
            value = UInt8(((hvalue - 0x41 + 10) * 16) & 0xFF);
        }
    
        if ((lvalue <= 0x39) && (lvalue >= 0x30)) {
            value |= UInt8((lvalue - 0x30) & 0xFF);
        } else {
            value |= UInt8((lvalue - 0x41 + 10) & 0xFF);
        }
    
        return value;
    }
    
    // string转换成Data
    public static func convertStringToByteArray(str:String) -> Data?{
    
        let str_tmp = str.uppercased();
        var hvalue:UInt8 = 0;
        var lvalue:UInt8 = 0;
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
        var str_nadata = str_tmp.data(using: String.Encoding.utf8);
        if(str_nadata == nil){
            return nil;
        }
        
        var str_byte = [UInt8](repeating: 0, count: str_nadata!.count);
        str_nadata?.copyBytes(to: &str_byte, count: str_byte.count);
        
        var str_result = [UInt8]();
        
        var i=0;
        while i < str_byte.count {
            hvalue = str_byte[i]; // 高位
            
            if (((hvalue <= 0x39) && (hvalue >= 0x30)) || ((hvalue <= 0x46) && (hvalue >= 0x41))) {
                if ((i + 1) >= str_byte.count) {
                    lvalue = 0x30;
                } else {
                    lvalue = str_byte[i + 1];
                    if (((lvalue <= 0x39) && (lvalue >= 0x30)) || ((lvalue <= 0x46) && (lvalue >= 0x41))) {
                        
                    } else {
                        lvalue = 0x30;
                    }
                }
                i += 2;
                str_result.append(convertCharToByte(hvalue_const: hvalue, lvalue_const: lvalue));
            } else {
                i += 1;
            }
        }

        if(str_result.count == 0){
            return nil;
        }
        
        return Data(bytes: str_result, count: str_result.count);
    }
    
    // Data转换成[UInt8]
    public static func converDataToByteArray(data:Data) -> [UInt8]{
        var bytes = [UInt8](repeating: 0, count: data.count);
        data.copyBytes(to: &bytes, count: bytes.count);
        return bytes;
    }
    
    public static func convertUint32(b0:UInt8, b1:UInt8, b2:UInt8, b3:UInt8) -> Int{
        var value:Int = 0;
        value |= Int(b3 & 0xFF);
        value <<= 8;
        value |= Int(b2 & 0xFF);
        value <<= 8;
        value |= Int(b1 & 0xFF);
        value <<= 8;
        value |= Int(b0 & 0xFF);
        return value;
    }
    
    public static func convertUint16(hb:UInt8, lb:UInt8) -> Int{
        var value:Int = 0;
        value |= Int(hb & 0xff);
        value <<= 8;
        value |= Int(lb & 0xff);
        return value;
    }
    
    public static func convertSint16(hb:UInt8, lb:UInt8) -> Int{
    
        var value = convertUint16(hb: hb, lb: lb);
        if (value >= 0x8000) {
            value = value - 0x10000;
        }
        return value;
    }
    
    public static func convertUint8(b:UInt8) -> Int{
        var value = 0;
        value |= Int(b & 0xff);
        return value;
    }
    
    public static func convertIntToByte(i:Int) -> UInt8{
        return UInt8(i & 0xFF);
    }
    
    public static func converLoUint16(i:Int) -> UInt8{
        return UInt8(i & 0xff);
    }
    
    public static func converHiUint16(i:Int) -> UInt8{
        return UInt8(((i>>8) & 0xff));
    }
    
    // byte数组截取
    public static func subBytes(datas:[UInt8], index:Int, lenght:Int) -> [UInt8]?{
        if ((index + lenght) >= datas.count) {
            return nil;
        }
    
        var dstDatas = [UInt8](repeating: 0, count: lenght);
        MTTools.arraycopy(src: datas, srcPos: index, dst: &dstDatas, dstPos: 0, length: dstDatas.count);
    
        return dstDatas;
    }
    
    // String 转 [UInt8]   UTF-8格式
    public static func stringToUint8UTF8(str:String) -> [UInt8]{
        let data = str.data(using: String.Encoding.utf8, allowLossyConversion: false);
        
        let hex_buffer = converDataToByteArray(data: data!);
        
        return hex_buffer;
    }
    
    // 拷贝数组
    public static func arraycopy(src:[UInt8], srcPos:Int, dst:UnsafeMutablePointer<UInt8>, dstPos:Int, length:Int){
        for i in 0 ..< length{
            dst[dstPos+i] = src[srcPos+i];
        }
    }
    
    // 打印信息
    public static func logout(log:String) {
        print(log);
    }
}
